package com.controller.xsxt;

import com.entity.common.ApiResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.service.xsxt.StudentService;
import com.entity.xsxt.Student;
import com.util.CommonUtil;
import com.util.Md5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by System on 2019/04/15 08:55:25.
 */
@RestController
@RequestMapping("student")
public class StudentController {

    @Autowired
    private StudentService studentService;

    @RequestMapping("/getPageList")
    public Map<String,Object> getPageList(@RequestParam(value = "pager.pageNo",defaultValue ="1" )  Integer pageNum,
                @RequestParam(value = "pager.pageSize",defaultValue ="10" ) Integer pageSize,Student student){
        Map<String, Object> map=new HashMap<>();
        PageHelper.startPage(pageNum,pageSize);
        PageInfo<Student> pageInfo = new PageInfo<>(studentService.getPageList(student));
        map.put("rows",pageInfo.getList());
        map.put("pager.totalRows", pageInfo.getTotal());
        map.put("pager.pageNo",pageNum);
        return  map;
    }

    @RequestMapping("/addStudent")
    public ApiResult addStudent(Student student){
        student.setStuCode(CommonUtil.getUUID());
        student.setPassword("123456");
        Md5Util.setHex(student);
        return studentService.addStudent(student);
    }

    @RequestMapping("/deleteStudent")
    public ApiResult deleteStudent(Long id){
        return studentService.deleteStudent(id);
    }

    @RequestMapping("/getStudentById")
    public ApiResult getStudentById(Long id){
        return studentService.getStudentById(id);
    }

    @RequestMapping("/editStudent")
    public ApiResult editStudent(Student student){
        return studentService.editStudent(student);
    }

    @RequestMapping("/studentLogin")
    public ApiResult studentLogin(Student model,HttpSession session){
         Md5Util.setHex(model);
         String password=model.getPassword();
         Student student=studentService.findStudentByStuno(model);
         if(student!=null){
             if(password.equals(student.getPassword())){
                 session.setAttribute("userInfo",student);
                return new ApiResult(true,"登录成功！",student);
             }
             else{
                 return new ApiResult(false,"密码错误！");
             }
         }
        return new ApiResult(false,"用户名或密码错误！");
    }

    @RequestMapping("/exitLogin")
    public void exitLogin(HttpSession session){
        session.removeAttribute("userInfo");
    }
    @RequestMapping("/checkStudentLoginState")
     public ApiResult checkStudentLoginState(HttpSession session){
         Student student= (Student) session.getAttribute("userInfo");
         if(student!=null){
             return new  ApiResult(true,"已经登录");
         }
         else{
             return new ApiResult(false,"用户没有登录！");
         }
     }

}
